<?php


namespace app\admin\controller\expert;


use app\common\controller\Adminbase;
use think\Db;

class Expertorder extends Adminbase
{
    protected $searchFields = 'name';
    protected $goods_categoty;

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        //商品类型 商品名称 所需积分 排序 库存 兑换数量  操作：删除 编辑 种类不更改
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['eo.is_del', '=', 0];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['eo.id', '=', $filter['id']];
                }
                if (isset($filter['is_js'])) {
                    $where[] = ['eo.is_js', '=', $filter['is_js']];
                }

                if (isset($filter['content_cate.name'])) {
                    $where[] = ['eo.content_cate_id', '=',  $filter['content_cate.name']  ];
                }

                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['mobile'])) {
                    $where[] = ['m.mobile', 'like', '%' . $filter['mobile'] . '%'];
                }
            }
            $_list = Db::name('expert_order')
                ->alias('eo')
                ->field('eo.*,m.nickname,m.mobile')
                ->join('member m','eo.member_id=m.id')
                ->order('eo.create_time desc')
                ->where($where)
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['member'] = Db::name('member')->find($v['member_id']);
                $_list[$k]['content_cate'] = Db::name('content_cate')->find($v['content_cate_id']);
            }
            $total = Db::name('expert_order')
                ->alias('eo')
                ->field('eo.*,m.nickname,m.mobile')
                ->join('member m','eo.member_id=m.id')
                ->where($where)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $_list);
            return json($result);
        }
        $goods_categoty = Db::name('content_cate')
            ->where('is_del', '<>', 1)
            ->order('sort desc')
            ->select();
        foreach ($goods_categoty as $k => $v) {
            $goods_categoty2[$v['id']] = $v['name'];
        }
        $this->assign('goodsCategoty', $goods_categoty2);
        return $this->fetch();
    }

    //增加
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $expert_order_data = array(
                'content_cate_id' => $data['content_cate_id'],
                'demand' => $data['demand'],
                'require' => $data['require'],
                'content' => $data['content'],
                'opinion' => $data['opinion'],
                'message_info' => $data['message_info'],
                'server_time' => $data['server_time'],
//                'sort' => $data['sort'],
                'create_time' => time(),
            );
            $expert_order_id = Db::name('expert_order')->insertGetId($expert_order_data);
            if ($expert_order_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $goods_cate = Db::name('content_cate')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        $this->assign([
            'goods_cate' => $goods_cate,
        ]);
        //商品分类
        return $this->fetch('add2');
    }

    /**
     * 状态
     */
    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('expert_order')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 推荐
     */
    public function recommend()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');

        if (Db::name('expert_order')->where('id', $id)->update(['recommend' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //删除
    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('expert_order')->where('id', $id)->update([
                    'is_del' => 1
                ]);
                Db::name('goods_attr')->where(['goods_id'=>$id])->delete();
                Db::name('goods_attr_value')->where(['goods_id'=>$id])->delete();
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $expert_order_data = array(
                'id'=>$data['id'],
                'content_cate_id' => $data['content_cate_id'],
                'demand' => $data['demand'],
                'require' => $data['require'],
                'content' => $data['content'],
                'opinion' => $data['opinion'],
                'message_info' => $data['message_info'],
                'server_time' => $data['server_time'],
                'create_time' => time(),
            );
            $expert_order_id = Db::name('expert_order')->update($expert_order_data);

            if ($expert_order_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $data = Db::name('expert_order')->find($id);
        $goods_cate = Db::name('content_cate')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();

        $this->assign([
            'goods_cate' => $goods_cate,
            'data' => $data
        ]);

        $this->assign('data', $data);
        return $this->fetch('');
    }

    public function editfee()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $update_data = array(
                'id' => $data['id'],
                'js_money' => $data['js_money'],
                'is_js'=>1,
                'js_time'=>time()
            );
            $stmt = Db::name('expert_order')->update($update_data);
            $expert_order=Db::name('expert_order')->find($id);

            addMoney($expert_order['member_id'],$data['js_money'],2,$id,4);
            $content='专家咨询消耗余额'.$data['js_money'].'元';
            sendMsg(2, '估价消耗余额', $content, $expert_order['member_id']);

            if ($stmt) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }
        $data = Db::name('expert_order')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }
}